function trprc = totalreturnprice(prc,act,div)
%TOTALRETURNPRICE Total return price time series.
%   TRPRC = TOTALRETURNPRICE(PRC,ACT,DIV) generates a total return price 
%   time series given price data, action or split data, and 
%   dividend data.  All input data is unadjusted.
%
%   Inputs:
%
%     PRC - An Mx2 matrix of price data where column 1 is MATLAB date 
%     numbers and column 2 is price values.
%
%     ACT - An Nx2 matrix of action or split data where column 1 is MATLAB date 
%     numbers and column 2 is split ratios.
%
%     DIV - An Px2 matrix of dividend data where column 1 is MATLAB date 
%     numbers and column 2 is cash dividend payouts.
%
%   Output:
%  
%     TRPRC - An Mx2 matrix of price data where column 1 is MATLAB date 
%     numbers and column 2 is total return price values.
%
%     See also PERIODICRETURNS.

%       Copyright 1995-2005 The MathWorks, Inc.
%       $Revision: 1.1.6.4 $   $Date: 2008/02/02 13:04:58 $

%Check input dimensions, prc must have two columns, act and div must either
%be empty or have two columns
try
  prccols = size(prc,2);
  actcols = size(act,2);
  divcols = size(div,2);
catch exception
  error('finance:totalreturnprice:dataInputError',exception.message)
end

if (prccols ~= 2)
  error('finance:totalreturnprice:priceDataInputError',...
        'Price data must be an Mx2 matrix of dates and prices.')
end
if (actcols ~= 0 && actcols ~= 2)
  error('finance:totalreturnprice:splitDataInputError',...
        'Action data must be empty or an Mx2 matrix of dates and ratios.')
end
if (divcols ~= 0 && divcols ~= 2)
  error('finance:totalreturnprice:divDataInputError',...
        'Dividend data must be empty or an Mx2 matrix of dates and dividend values.')
end

%Sort data
prc = sortrows(prc);
act = sortrows(act);
div = sortrows(div);

% get number of dates from price data
maxindex = size(prc,1);

% set up date indexing for the prc, div, and act arrays
pindex = 1;
pdate = prc(pindex,1);

if size(div,1) > 0
    dindex = 1;
    ddate = div(dindex,1);
end

if size(act,1) > 0
    aindex = 1;
    adate = act(aindex,1);
end

% calculate total return prices and put into trprc array

trprc = zeros(maxindex,2);
trprc(1,:) = [pdate, 1];

for pindex = 2:maxindex

    pdate = prc(pindex,1);
    trval = prc(pindex,2);

    %Dividend data handling
    if size(div,1) > 0
        if (ddate == pdate) && (dindex <= size(div,1))
           
            trval = trval + div(dindex,2);
            
            dindex = dindex + 1;
            if dindex <= size(div,1)
                ddate = div(dindex,1);
            end
        end
    end
    
    trval = trval / prc(pindex - 1,2);

    %Split data handling
    if size(act,1) > 0
        if (adate == pdate) && (aindex <= size(act,1))
            
            trval = trval * act(aindex,2);
        
            aindex = aindex + 1;
            if aindex <= size(act,1)
                adate = act(aindex,1);
            end
        end
    end
    
    trval = trval * trprc(pindex - 1,2);
    
    trprc(pindex,:) = [pdate, trval];
end
